using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Security.Cryptography;
using System.IO;

namespace TracClient
{
  public class Config
  {
    public Config()
    {
    }

    public string site = "http://trac.edgewall.org/";
    public string user = "anonymous";
    public string pass = string.Empty;

    public string listUrl = "report/12?format=csv&USER=";
    public bool appendUserToListUrl = true;
    public bool hideTicketsWithClosedStatus = false;
    public bool hideTicketsWithFixedStatus = false;

    public string GetFileName()
    {
      return loadedFromFile;
    }

    public void Save()
    {
      if (loadedFromFile.Length == 0)
        return;
      SaveAs(loadedFromFile);
    }

    public void SaveAs(string file)
    {
      try
      {
        XmlSerializer serializer = new XmlSerializer(typeof(Config));
        using (StreamWriter writer = new StreamWriter(file))
          serializer.Serialize(writer, this);
      }
      catch
      {
      }
    }

    static public Config Load(string file)
    {
      Config config = new Config();
      config.loadedFromFile = file;
      if (!File.Exists(file))
        return config;
      try
      {
        XmlSerializer serializer = new XmlSerializer(typeof(Config));
        
        using (StreamReader reader = new StreamReader(file))
        {
          config = serializer.Deserialize(reader) as Config;
          config.loadedFromFile = file;
        }
        return config;
      }
      catch
      {
        return config;
      }
    }

    [XmlIgnore]
    private string loadedFromFile = string.Empty;
    [XmlIgnore]
    static public Config main = new Config();
  }
}
